package cn.csust.net2.manager.server.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.csust.net2.manager.server.dao.BaseDAO;
import cn.csust.net2.manager.server.remote.BeanUtils;
import cn.csust.net2.manager.server.remote.RemoveServiceBean;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.service.BeanService;
import cn.csust.net2.manager.shared.util.ServiceConstant;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.FilterConfig;
import com.extjs.gxt.ui.client.data.FilterPagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
/**
 * 
 * @author 韩忠金
 *
 * @param <T>
 */
@Service(value=ServiceConstant.SERVICE_NAME_BEAN)
public  class  BeanServiceImpl<T extends PO> implements BeanService<T> ,RemoveServiceBean{
	protected HttpServletRequest request;
	//BaseService接口定义接收REQUEST对象的方法
	/**
	 * synchronized关键字牺牲时间解决线程不安全
	 */
	public synchronized void loadRequest(HttpServletRequest request){
		this.request = request;
	}
	@Transactional
	@Override
	public synchronized PagingLoadResult<T> findByPaging(String cla, T t,PagingLoadConfig config) throws BeyondAuthorityException{
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		
		return dao.findByPaging(t, config);
	}
	@Transactional
	@Override
	public synchronized PagingLoadResult<T> findAllByPaging(String cla, PagingLoadConfig config)throws BeyondAuthorityException {
		Student student = config.get("student");
		StringBuffer s = new StringBuffer(" from "+cla+" c where 1=1 ");
		
		if(student!=null){
			System.out.println("加载表格的用户名为"+student.getName());
			s.append(" and c.user.userID = "+student.getUserID());
		}
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		 BasePagingLoadResult<T> list = dao.findAllByPaging(config,s.toString());
		return list;
		
	}
	@Transactional
	@Override
	public synchronized void save(String cla, T t)throws BeyondAuthorityException {
		
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		dao.save(t);
	}
	@Transactional
	@Override
	public void save(String cla, List<T> list)throws BeyondAuthorityException {
		// TODO Auto-generated method stub
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		for(T t : list) {
			dao.save(t);
		}
	}
	@Transactional
	@Override
	public synchronized void delete(String cla, T t) throws BeyondAuthorityException{
		
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		dao.delete(t);
		
	}
	@Transactional
	@Override
	public synchronized void update(String cla, T t) throws BeyondAuthorityException{
		
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		dao.update(t);
	}
	@Transactional
	@Override
	public synchronized List<T> findAll(String cla)throws BeyondAuthorityException {

		BaseDAO<T> dao = BeanUtils.getBean(cla);
		return dao.findAll();
	}
	@Transactional
	@Override
	public synchronized List<T> findByExample(String cla, T t)throws BeyondAuthorityException {
		
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		return dao.findListByExample(t);
	}
	@Transactional
	@Override
	public synchronized T findByUniqueExample(String cla, T t)throws BeyondAuthorityException {
		
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		return dao.findUniqueByExample(t);
	}
	@Transactional
	@Override
	public synchronized long counts(String cla,T t) throws BeyondAuthorityException{
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		return dao.counts(t);
	}
	@Transactional
	@Override
	public synchronized void update(String cla, List<T> list)throws BeyondAuthorityException {
		// TODO Auto-generated method stub
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		for(T t : list) {
			dao.update(t);
		}
	}
	@Transactional
	@Override
	public synchronized void delete(String cla, List<T> list)throws BeyondAuthorityException {
		// TODO Auto-generated method stub
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		for(T t : list) {
			dao.delete(t);
		}
	}

	@Transactional
	@Override
	public synchronized PagingLoadResult<T> findAllByPaging(String cla,
			FilterPagingLoadConfig config) throws BeyondAuthorityException{
		// TODO Auto-generated method stub
		String shuai =  config.get("hanzhongjin");
		System.out.println(shuai+"wwwwwwwwwww");
		List<FilterConfig> filters = config.getFilterConfigs();
		if(filters!=null){
	    for (FilterConfig f : filters) {
	      Object ov = f.getValue();
	      String com = f.getComparison();
	      String property = f.getField();
	      String type = f.getType();
	      System.out.println("value:"+ov+",com = "+com+",property="+property+",type= "+type);
	    }
		
		}
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		return dao.findAllByPaging(config);
	   
	}
	
	
}
